package org.mms.controller;

import org.mms.pojo.dto.EmployeeDto;
import org.mms.pojo.entity.Employee;
import org.mms.pojo.request.EmployeeRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public ResponseMessage<List<EmployeeDto>> list() {
        return ResponseMessage.success(employeeService.list()
                .stream()
                .map(Employee::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<EmployeeDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(employeeService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<EmployeeDto> create(@RequestBody EmployeeRequest request) {
        return ResponseMessage.success(employeeService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<EmployeeDto> update(@RequestBody EmployeeRequest request) {
        return ResponseMessage.success(employeeService.update(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<EmployeeDto> delete(@PathVariable Long id) {
        employeeService.delete(id);
        return ResponseMessage.success(null);
    }
}